<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;

class Login extends BaseController
{
    public function login()
    {
        $fieldsSelected = getSelectParams('phone,pwd');
        $info = Db::name('users')->field('id,phone')->where($fieldsSelected)->find();

        if ($info) {
            $info['token'] = createUserToken($info['id']);
        }
        //     print_r(  $info);
        //  print_r(Db::name('users').fetchSql());
        singleJson($info, '账号或密码错误','',false);
    }

    public function reg()
    {
        $phone = Request::param('phone');

        $info = Db::name('users')->field('id')->where(array('phone' => $phone))->find();
 //   print_r($info);
        if ($info) {
            singleJson(false, '手机号已被使用');
        }

        $fieldsSelected = getSelectParams('phone,pwd');

      $lastid =  Db::name('users')->insert($fieldsSelected);
      if(!$lastid){
          singleJson(false, '账号或密码错误');
      }

        $info['phone'] = $phone;

        $info['token'] = createUserToken($lastid);

        //  print_r(Db::name('users').fetchSql());
        singleJson($info, '账号或密码错误');
    }

    // http://www.penyou.com/pics/lists

    public function lists()
    {
        $lists = Db::name('pics')->select();
        // echo json_encode(ara)
        // echo json_encode($lists);
        $arr = array(
            'id' => 1,
            'data' => $lists
        );
        echo json_encode($arr);
    }
}
